var EmployeeApp = function(){
	var dPage = 1,dPageSize=5;
	var flag = false;
	return{
		login:function(){
			var phone = $.trim($("#phone").val());
			var passwd = $.trim($("#passwd").val());
			
			if(phone == ""){
				$("#phone").focus();
				layer.open({content: '手机号码不能为空',skin: 'msg',time: 2});
				return;
			}else if(!mui.util.StringUtil.isPhone(phone)){
				$("#phone").focus();
				layer.open({content: '手机号码格式不正确',skin: 'msg',time: 2});
				return;
			}
			
			if(passwd == ""){
				$("#passwd").focus();
				layer.open({content: '密码不能为空',skin: 'msg',time: 2});
				return;
			}
			
			var data = {phone:phone,passwd:passwd};
			var url = $("#APP_ROOT_PATH").val() + 'employee/login_data';
			mui.util.Ajax.asyncGET(url,data,function(res){
				if(res.ret != undefined && res.ret != 0){
					layer.open({
					    content: res.message,
					    skin: 'msg',
					    time: 2 //2秒后自动关闭
					  });
					return;
				}
				
				if(res){
					window.location.href = $("#APP_ROOT_PATH").val() + 'employee/index';
				}else{
					layer.open({
					    content: '登录失败',
					    skin: 'msg',
					    time: 2 //2秒后自动关闭
					  });
					return;
				}
			})
		},
		forgetPass:function(){
			var phone = $.trim($("#phone").val());
			if(phone == ""){
				$("#phone").focus();
				layer.open({content: '手机号码不能为空',skin: 'msg',time: 2});
				return;
			}else if(!mui.util.StringUtil.isPhone(phone)){
				$("#phone").focus();
				layer.open({content: '手机号码格式不正确',skin: 'msg',time: 2});
				return;
			}
			
			var url = $("#APP_ROOT_PATH").val() + 'employee/forget';
			var data = {phone:phone};
			mui.util.Ajax.asyncGET(url,data,function(res){
				if(res.ret != undefined && res.ret != 0){
					layer.open({
					    content: res.message,
					    skin: 'msg',
					    time: 2 //2秒后自动关闭
					  });
					return;
				}
				
				if(res){
					layer.open({
					    content: '短信发送成功',
					    skin: 'msg',
					    time: 2 //2秒后自动关闭
					  });
					return;
				}else{
					layer.open({
					    content: '找回密码失败',
					    skin: 'msg',
					    time: 2 //2秒后自动关闭
					  });
					return;
				}
			})
		},
		changePasswd:function(){
			var phone = $.trim($("#phone").val());
			var oldPasswd = $.trim($("#oldPasswd").val());
			var passwd = $.trim($("#passwd").val());
			var confirmPasswd = $.trim($("#confirmPass").val());
			
			if(phone == ""){
				$("#phone").focus();
				layer.open({content: '手机号码不能为空',skin: 'msg',time: 2});
				return;
			}else if(!mui.util.StringUtil.isPhone(phone)){
				$("#phone").focus();
				layer.open({content: '手机号码格式不正确',skin: 'msg',time: 2});
				return;
			}
			
			if(oldPasswd == ""){
				$("#oldPasswd").focus();
				layer.open({content: '手机密码不能为空',skin: 'msg',time: 2});
				return;
			}else if(oldPasswd.length >= 20){
				$("#oldPasswd").focus();
				layer.open({content: '手机密码长度不能大于20位',skin: 'msg',time: 2});
				return;
			}
			
			if(passwd == ""){
				$("#passwd").focus();
				layer.open({content: '新密码不能为空',skin: 'msg',time: 2});
				return;
			}else if(passwd.length >= 20){
				$("#passwd").focus();
				layer.open({content: '新密码长度不能大于20位',skin: 'msg',time: 2});
				return;
			}
			
			if(passwd != confirmPasswd){
				$("#passwd").focus();
				layer.open({content: '两次输入的密码不一致',skin: 'msg',time: 2});
				return;
			}
			
			var url = $("#APP_ROOT_PATH").val() + 'employee/save_new_passwd';
			var data = {phone:phone,passwd:confirmPasswd,oldPasswd:oldPasswd};
			mui.util.Ajax.asyncGET(url,data,function(res){
				if(res.ret != undefined && res.ret != 0){
					layer.open({
					    content: res.message,
					    skin: 'msg',
					    time: 2 //2秒后自动关闭
					  });
					return;
				}
				
				if(res){
					window.location.href = $("#APP_ROOT_PATH").val() + 'employee/login';
				}else{
					layer.open({
					    content: '修改失败',
					    skin: 'msg',
					    time: 2 //2秒后自动关闭
					  });
					return;
				}
			})
			
		},
		loadArticle:function(p){
			if(!!!p)p=1;
			
			var data = {page:p,pageSize:dPageSize};
			var url = $("#APP_ROOT_PATH").val() + 'employee/load_deposit';
			mui.util.Ajax.asyncGET(url,data,function(res){
				if(res.ret != undefined && res.ret != 0){
					layer.open({
					    content: res.message,
					    skin: 'msg',
					    time: 2 //2秒后自动关闭
					  });
					return;
				}
				
				var loaded = (p - 1) * dPageSize + res.rows.length;
				if(res.rows.length <= 0 || loaded >= res.total){
					$(".bottom-1").text('没有更多订单啦');
				}else{
					$(".bottom-1").text('');
				}
				
				var data = {data:res.rows};
				var bt=baidu.template;
				var html=bt('t:bt',data);
				$("#data-content .bottom-1").before(html);
				dPage++;
				
				if($("#data-content")[0].scrollHeight<=$("#data-content").height()){
					if(!flag){
						flag = true;
						EmployeeApp.loadArticle(dPage)
					}
				}else{
					EmployeeApp.bindScrollEvent();
				}
			});
		},
		bindScrollEvent:function(){
			$('#data-content').unbind('scroll');
			
			$('#data-content').on('scroll',function(){
				var aH = $("#data-content")[0].scrollHeight
				var h = $("#data-content").height()
				var st = $('#data-content').scrollTop()
			    if (aH==h+st) {
			    	EmployeeApp.loadArticle(dPage);
			    }
			});
		}
	}
}();